package com.dc.controller;

import com.dc.baseController.BaseController;
import com.dc.em.ErrorMessageEnum;
import com.dc.model.Order;
import com.dc.model.ShopOrder;
import com.dc.model.User;
import com.dc.service.OrderService;
import com.dc.service.ShopOrderService;
import com.dc.vo.BaseModel;
import com.dc.vo.BusinessException;
import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiImplicitParam;
import com.wordnik.swagger.annotations.ApiImplicitParams;
import com.wordnik.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/Order")
@Api("订单模块")
public class OrderController extends BaseController {
    @Autowired
    OrderService orderService;
    @Autowired
    ShopOrderService shopOrderService;
    @RequestMapping(value = "/selectPageOrder",method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(value = "用户名",name = "user_name",required = false,dataType = "String",paramType = "query"),
            @ApiImplicitParam(value = "当前页面",name = "curr_page",defaultValue = "1",dataType = "Integer",paramType = "query"),
            @ApiImplicitParam(value = "一页的最大条数",name = "page_size",defaultValue = "10",dataType = "Integer",paramType = "query")
    })
    @ResponseBody
    @ApiOperation(value = "分页查询订单信息",httpMethod = "POST",notes = "查询")
    public BaseModel selectPageOrder(String user_name, Integer curr_page, Integer page_size, BaseModel baseModel, HttpServletRequest request) throws Exception{
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("currentUser");
        if(user == null) {
            throw new BusinessException(ErrorMessageEnum.LOGIN_TIMEOUT);
        }
        return orderService.selectPageOrder(user_name, curr_page, page_size, baseModel);
    }
    @RequestMapping(value = "/addOrder",method = RequestMethod.POST)
    @ApiOperation(value = "新增订单",httpMethod = "POST",notes = "新增")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "商品id",name = "shoes_id",required = true,dataType = "Integer",paramType = "query"),
            @ApiImplicitParam(value = "收货地址",name = "address_id",required = true,dataType = "Integer",paramType = "query"),
            @ApiImplicitParam(value = "支付类型",name = "pay_type",required = true,dataType = "Integer",paramType = "query")
    })
    @ResponseBody
    public BaseModel addOrder(Order order, BaseModel baseModel, HttpServletRequest request) throws Exception{
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("currentUser");
        if(user == null){
            throw new BusinessException(ErrorMessageEnum.LOGIN_TIMEOUT);
        }
        order.setUser_id(user.getUser_id());
        order.setUser_name(user.getName());
        order.setPhone(user.getPhone());
        order.setUser_id(user.getUser_id());
        order.setState(0);
        order.setIs_deleted(0);
        order.setVersion(0);
        return orderService.addOrder(order,baseModel);
    }
    @RequestMapping(value = "/deletedOrder/{ids}",method = RequestMethod.POST)
    @ApiOperation(value="删除订单",httpMethod = "POST",notes = "删除")
    @ResponseBody
    public BaseModel deletedShoes(@PathVariable("ids") String ids, BaseModel baseModel, HttpServletRequest request) throws Exception{
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("currentUser");
        if(user == null){
            throw new BusinessException(ErrorMessageEnum.LOGIN_TIMEOUT);
        }
        return orderService.deletedOrder(ids,baseModel);
    }
    @RequestMapping(value = "/updateOrder",method = RequestMethod.POST)
    @ApiOperation(value="修改订单",httpMethod = "POST",notes = "修改")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "订单ID",name = "order_id",required = true,dataType = "Integer",paramType = "query"),
            @ApiImplicitParam(value = "用户姓名名称",name = "user_name",required = false,dataType = "String",paramType = "query"),
            @ApiImplicitParam(value = "手机号",name = "phone",required = false,dataType = "String",paramType = "query")
    })
    @ResponseBody
    public BaseModel updateOrder(Order order,BaseModel baseModel, HttpServletRequest request) throws Exception{
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("currentUser");
        if(user == null){
            throw new BusinessException(ErrorMessageEnum.LOGIN_TIMEOUT);
        }
        return orderService.updateOrder(order,baseModel);
    }
    @RequestMapping(value = "/selectOrderById/{id}",method = RequestMethod.POST)
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(value = "当前页面",name = "curr_page",defaultValue = "1",dataType = "Integer",paramType = "query"),
            @ApiImplicitParam(value = "一页的最大条数",name = "page_size",defaultValue = "10",dataType = "Integer",paramType = "query")
    })
    @ApiOperation(value = "按用户id订单信息",httpMethod = "POST",notes = "查询")
    public BaseModel selectOrderById(@PathVariable(value = "id") int id, Integer curr_page,Integer page_size,BaseModel baseModel, HttpServletRequest request) throws Exception{
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("currentUser");
        if(user == null) {
            throw new BusinessException(ErrorMessageEnum.LOGIN_TIMEOUT);
        }
        return orderService.selectOrderById(id, baseModel, curr_page, page_size);
    }
    @RequestMapping(value = "/selectOrderByid/{id}",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "按订单id订单信息",httpMethod = "POST",notes = "查询")
    public BaseModel selectOrderByid(@PathVariable(value = "id") int id, BaseModel baseModel, HttpServletRequest request) throws Exception{
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("currentUser");
        if(user == null) {
            throw new BusinessException(ErrorMessageEnum.LOGIN_TIMEOUT);
        }
        return orderService.selectOrderByid(id, baseModel);
    }
    @RequestMapping(value = "/addShopOrder",method = RequestMethod.POST)
    @ApiOperation(value = "新增订单中间表",httpMethod = "POST",notes = "新增")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "商品id",name = "shoes_id",required = true,dataType = "Integer",paramType = "query"),
            @ApiImplicitParam(value = "商品数量",name = "shoes_num",required = true,dataType = "Integer",paramType = "query")
    })
    @ResponseBody
//    新增订单中间表
    public BaseModel addShopOrder(ShopOrder shopOrder, BaseModel baseModel, HttpServletRequest request) throws Exception{
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("currentUser");
        if(user == null){
            throw new BusinessException(ErrorMessageEnum.LOGIN_TIMEOUT);
        }
        return shopOrderService.addShopOrder(shopOrder,baseModel);
    }
    @RequestMapping(value = "/selectOrderInfoById/{id}",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "查询订单详情信息",httpMethod = "POST",notes = "查询")
//    查询订单详情信息
    public BaseModel selectOrderInfoById(@PathVariable("id") int id, BaseModel baseModel, HttpServletRequest request) throws Exception{
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("currentUser");
        if(user == null) {
            throw new BusinessException(ErrorMessageEnum.LOGIN_TIMEOUT);
        }
        return orderService.selectOrderInfoById(id, baseModel);
    }
    @RequestMapping(value = "/selectPageNOPassOrder/{id}",method = RequestMethod.POST)
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(value = "当前页面",name = "curr_page",defaultValue = "1",dataType = "Integer",paramType = "query"),
            @ApiImplicitParam(value = "一页的最大条数",name = "page_size",defaultValue = "10",dataType = "Integer",paramType = "query")
    })
    @ApiOperation(value = "查询未通过订单",httpMethod = "POST",notes = "查询")
//    查询订单详情信息
    public BaseModel selectPageNOPassOrder(@PathVariable("id") int id,Integer curr_page,Integer page_size,BaseModel baseModel, HttpServletRequest request) throws Exception{
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("currentUser");
        if(user == null) {
            throw new BusinessException(ErrorMessageEnum.LOGIN_TIMEOUT);
        }
        return orderService.selectPageNOPassOrder(id,curr_page,page_size,baseModel);
    }
    @RequestMapping(value = "/selectPagePassOrder/{id}",method = RequestMethod.POST)
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(value = "当前页面",name = "curr_page",defaultValue = "1",dataType = "Integer",paramType = "query"),
            @ApiImplicitParam(value = "一页的最大条数",name = "page_size",defaultValue = "10",dataType = "Integer",paramType = "query")
    })
    @ApiOperation(value = "查询已通过订单",httpMethod = "POST",notes = "查询")
//    查询订单详情信息
    public BaseModel selectPagePassOrder(@PathVariable("id") int id,Integer curr_page,Integer page_size,BaseModel baseModel, HttpServletRequest request) throws Exception{
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("currentUser");
        if(user == null) {
            throw new BusinessException(ErrorMessageEnum.LOGIN_TIMEOUT);
        }
        return orderService.selectPagePassOrder(id,curr_page,page_size,baseModel);
    }
//    审核订单
        @RequestMapping(value = "/updateOrderState/{id}",method = RequestMethod.POST)
        @ResponseBody
        @ApiImplicitParams({
                @ApiImplicitParam(value = "状态", name = "state", required = true, dataType = "int", paramType = "query"),
        })
        @ApiOperation(value = "查询已通过订单",httpMethod = "POST",notes = "查询")
//    查询订单详情信息
        public BaseModel updateOrderState(@PathVariable("id") int id,int state,BaseModel baseModel, HttpServletRequest request) throws Exception{
            HttpSession session = request.getSession();
            User user = (User) session.getAttribute("currentUser");
            if(user == null) {
                throw new BusinessException(ErrorMessageEnum.LOGIN_TIMEOUT);
                 }
            return orderService.updateOrderState(id,state,baseModel);
        }
    }
